package com.anjubao.parking.municipal.parkingrecord.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.EntryEnterDTO;
import com.anjubao.parking.municipal.parkinglot.dto.EntryLeaveDTO;
import com.anjubao.parking.municipal.parkinglot.dto.EntryResDTO;
import com.anjubao.parking.municipal.parkinglot.dto.EntryShotUrlDTO;
import com.anjubao.parking.municipal.parkinglot.feign.ParkingRecordFeignService;
import com.anjubao.parking.municipal.parkingrecord.service.ParkingRecordServiceImpl;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 车场本地系统对接相关接口
 * 进出记录上传API接口
 *
 * @author anjubao
 */
@RestController
@RequestMapping("/parking-records")
@Api(value = "进出记录接入上传API",tags ={ "车辆进出场API"})
public class ParkingRecordController implements ParkingRecordFeignService {

    @Autowired
    private ParkingRecordServiceImpl parkingRecordService;
    private static final Logger logger = LoggerFactory.getLogger(ParkingRecordController.class);

    /**
     * 车辆入场记录上报接口
     *
     * @param entryEnterDTO
     * @return
     */
    @ApiOperation(value = "车辆入场记录上报接口", notes = "车辆入场信息上传")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PostMapping("")
    @Override
    public ApiResult<EntryResDTO> enter(@RequestBody @Valid @ApiParam(value = "车辆入场信息") EntryEnterDTO entryEnterDTO) {
        logger.info("车辆入场信息上传 {} ", entryEnterDTO);
        //保存入场记录
        EntryResDTO entryResDTO = parkingRecordService.addEnterRecord(entryEnterDTO);
        //返回数据库操作结果
        return ApiResult.success(entryResDTO);
    }

    /**
     * 车辆出场记录上报接口
     *
     * @param entryLeaveDTO
     * @return
     */
    @ApiOperation(value = "车辆出场记录上报接口", notes = "车辆出场信息上传")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PutMapping("")
    @Override
    public ApiResult<EntryResDTO> leave(@RequestBody @Valid @ApiParam(value = "车辆出场信息") EntryLeaveDTO entryLeaveDTO) {
        logger.info("车辆出场信息上传 {} ", entryLeaveDTO);
        //保存出场记录
        EntryResDTO entryResDTO = parkingRecordService.updateLeave(entryLeaveDTO);
        //返回数据库操作结果
        return ApiResult.success(entryResDTO);
    }

    /**
     * 车辆进出图片URL上报接口
     *
     * @param entryShotUrlDTO
     * @return
     */
    @ApiOperation(value = "车辆进出图片URL上报接口", notes = "车辆进出图片URL信息上传")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })

    @PostMapping("/snapshot-url")
    @Override
    public ApiResult<EntryResDTO> snapShotUrl(@RequestBody @Valid @ApiParam(value = "车辆进出图片URL信息") EntryShotUrlDTO entryShotUrlDTO) {
        logger.info("车辆进出图片URL信息上传 {} ", entryShotUrlDTO);
        //判断本地车场记录id和云平台记录id是否为空
        if (entryShotUrlDTO.getLocalRecordId() == null && entryShotUrlDTO.getParkingRecordCode() == null) {
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "本地车场记录ID和云平台记录ID不能同时为空");
        }
        //保存进出场图片url
        EntryResDTO entryResDTO = parkingRecordService.updateShotUrl(entryShotUrlDTO);
        //返回数据库操作结果
        return ApiResult.success(entryResDTO);
    }
}
